﻿using System.ServiceProcess;
using CID.Execution.Agent.Service.Configuration;
using CID.Execution.Support.Contracts;
using CID.Execution.Support.PackageHandling;
using CID.Execution.Support.PackageHandling.Configuration;
using CID.Execution.Support.TemplateHandling;
using CID.Execution.Support.TemplateHandling.Configuration;
using NailsFramework;

namespace CID.Execution.Agent.Service
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ConfigureNails();
            ConfigurePackageHandlers();
            ConfigureTemplateHandlers();

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
			{ 
				new AgentWindowsService() 
			};
            ServiceBase.Run(ServicesToRun);
        }

        private static void ConfigureNails()
        {
            Nails.Configure()
                .Persistence.DataMapper<NailsFramework.Persistence.NullDataMapper>()
                .UserInterface.Platform<NailsFramework.UserInterface.NullUIPlatform>()
                .IoC.Container<NailsFramework.IoC.Unity>()
                .Logging.Logger<NailsFramework.Logging.Log4net>()
                .InspectAssemblyOf<IAgentCoordinator>()
                .InspectAssemblyOf<AgentServiceWCFWrapper>()
                .InspectAssemblyOf<IAgentService>()
                .Initialize();
        }

        private static void ConfigurePackageHandlers()
        {
            foreach (PackageHandlerElement packageHandlerConfiguration in ApplicationConfiguration.PackageHandlingConfiguration.Handlers)
                PackageHandlerFactory.Instance.RegisterPackageHandlerType(packageHandlerConfiguration.PackageTypeName, packageHandlerConfiguration.HandlerType);
        }

        private static void ConfigureTemplateHandlers()
        {
            foreach (TemplateHandlerElement templateHandlerConfiguration in ApplicationConfiguration.TemplateHandlingConfiguration.Handlers)
                TemplateHandlerFactory.Instance.RegisterTemplateHandlerType(templateHandlerConfiguration.TemplateTypeName, templateHandlerConfiguration.HandlerType);
        }
    }
}
